﻿// -----------------------------------------------------------------------
// <copyright file="ProjectItemSelector.cs" company="Microsoft">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace Rosieks.VisualStudio.Web.Flow.CustomCode
{
    using Microsoft.VisualStudio.OLE.Interop;
    using Microsoft.VisualStudio.Shell.Interop;
    using Rosieks.VisualStudio.Web.Flow.Interop;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class ViewHelpers
    {
        public static string SelectView()
        {
            //var asm = Assembly.Load("Microsoft.VisualStudio.Web.Mvc.3.01");

            //string x = asm.GetType("Microsoft.VisualStudio.Web.Mvc.Util.ViewHelpers").GetMethod("SelectLayout").Invoke(null, new object[] { ".webflow" }) as string;

            IVsHierarchy selectedHierarchy = WebflowUtil.GetSelectedHierarchy();
            if (selectedHierarchy != null)
            {
                bool canceled;
                string appRelUrlOfSelectItem;
                ProjectItemSelector.SelectItem(selectedHierarchy, "*.webflow", "Wybierz widok dla stanu", out appRelUrlOfSelectItem, out canceled);
                if ((!canceled && !string.IsNullOrEmpty(appRelUrlOfSelectItem)) && VsHierarchyItem.CreateFromMonikerOnly(appRelUrlOfSelectItem.Substring(2)) != null)
                {
                    return appRelUrlOfSelectItem;
                }
            }

            return string.Empty;
        }
    }

    internal class ProjectItemSelector
    {
        public static int SelectItem(IVsHierarchy hierarchy, string filter, string title, out string appRelUrlOfSelectedItem, out bool canceled)
        {
            appRelUrlOfSelectedItem = null;
            canceled = false;
            int num = int.MinValue;
            if (hierarchy != null)
            {
                IServiceProvider ppSP = null;
                hierarchy.GetSite(out ppSP);
                if (ppSP != null)
                {
                    IProjectItemSelector selector = WebflowUtil.CreateSitedInstance<IProjectItemSelector>(ppSP, typeof(ProjectItemSelector_Class).GUID);
                    if (selector != null)
                    {
                        num = selector.SelectItem(hierarchy, uint.MaxValue, filter, title, ProjectItemSelectorFlags.PISF_ReturnAppRelativeUrls, null, "~/", null, out appRelUrlOfSelectedItem, out canceled);
                    }
                }
            }

            return num;
        }
    }
}
